package patternlearning;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

public class ReasonPair {
	private String medName;
	private String reason;
	private String sentence;
	public ReasonPair()
	{
		this.medName = "";
		this.reason = "";
		this.sentence = "";
	}
	public ReasonPair(String medName,String reason)
	{
		this.medName = medName;
		this.reason = reason;
		this.sentence = "";
		this.medName = this.medName.toLowerCase();
		this.reason = this.reason.toLowerCase();
	}
	public ReasonPair(String medName,String reason,String sentence)
	{
		this.medName = medName;
		this.reason = reason;
		this.sentence = sentence;
		this.medName = this.medName.toLowerCase();
		this.reason = this.reason.toLowerCase();
	}
	public void ReadExtractedLine(String line)
	{
		String[] parts = line.split("\\|");
		this.medName = parts[0].trim();
		this.reason = parts[1].replaceAll("Reason=\"|\"", "");
		this.reason = this.reason.trim();
		this.sentence = parts[2].replaceAll("sentece=\"|\"", "");
		this.sentence = this.sentence.trim();
		this.medName = this.medName.toLowerCase();
		this.reason = this.reason.toLowerCase();
	}
	public void ReadGrountTruthLine(String line, String filename)
	{
		int pos = filename.indexOf('.');
		if(pos>0)filename = filename.substring(0, pos);
		String[] parts = line.split("\\|\\|");
		String mPart = parts[0];
		String rPart = parts[5];
		parts = mPart.split("\"");
		this.medName = parts[1];
		parts = rPart.split("\"");
		this.reason = parts[1];
		this.sentence = filename+" "+parts[2]+" ";
		try {
			BufferedReader freader = new BufferedReader(new FileReader("data/i2b2/"+filename));
			Vector<String> lines = new Vector<String>();
			String fileline;
			while((fileline=freader.readLine())!=null)
			{
				lines.add(fileline);
			}
			freader.close();
			pos = parts[2].indexOf(':');
			String lineNumStr = parts[2].substring(0, pos);
			int lineNum = Integer.parseInt(lineNumStr.trim());
			int beginLineNum = lineNum-2;
			int endLineNum = lineNum+2;
			if(beginLineNum<1) beginLineNum=1;
			if(endLineNum>lines.size()) endLineNum=lines.size();
			for(int i= beginLineNum-1;i<endLineNum;i++)
			{
				this.sentence += lines.elementAt(i)+" ";
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.medName = this.medName.toLowerCase();
		this.reason = this.reason.toLowerCase();
	}
	public String getMedName()
	{
		return this.medName;
	}
	public String getReason()
	{
		return this.reason;
	}
	public String getSentence()
	{
		return this.sentence;
	}
	/*public boolean equals(ReasonPair pair)
	{
		return this.medName.equals(pair.getMedName())&&this.reason.equals(pair.getReason());
	}*/
	public boolean equals(ReasonPair pair)
	{
		String otherName = pair.getMedName();
		String otherReason = pair.getReason();
		otherReason = otherReason.replaceAll("\\.", "");
		otherReason = otherReason.trim();
		return (this.medName.contains(otherName)||otherName.contains(this.medName))&&(this.reason.contains(otherReason)||otherReason.contains(this.reason));
	}
	public String toString()
	{
		String str = this.medName+"|Reason=\""+this.reason+"\""+"  sentence=\""+this.sentence+"\"";
		return str;
	}
	
}
